﻿using System;
using System.Threading;
using System.Threading.Tasks;

namespace Listing1_44
{
    public class Program
    {
        static void Main()
        {
            CancellationTokenSource cancellationTokenSource =
            new CancellationTokenSource();
            CancellationToken token = cancellationTokenSource.Token;
            Task task = Task.Run(() =>
            {
                while (!token.IsCancellationRequested)
                {
                    Console.Write("*");
                    Thread.Sleep(1000);
                }
            }, token).ContinueWith((t) =>
            {
                t.Exception.Handle((e) => true);
                Console.WriteLine("You have canceled the task");
            }, TaskContinuationOptions.OnlyOnCanceled);

            Console.WriteLine("Press enter to end the application");
            Console.ReadLine();
            Console.ReadKey();
        }
    }
}
// Displays
// Press enter to stop the task
// **
// A task was canceled.
// Press enter to end the application